﻿using System;
using System.Collections.Generic;

namespace Douya.Json
{
    public class ArrayBuilder
    {
        List<IJsonValue> lst;

        public ArrayBuilder()
        {
            lst = new List<IJsonValue>();
        }

        public int Count {
            get { return lst.Count; }
        }

        public ArrayBuilder AddNull(string name)
        {
            lst.Add(new JsonNull());

            return this;
        }

        public ArrayBuilder Add(bool value)
        {
            lst.Add(new JsonBoolean(value));

            return this;
        }

        public ArrayBuilder Add(int value)
        {
            lst.Add(new JsonInteger(value));

            return this;
        }

        public ArrayBuilder Add(string value)
        {
            if (value != null)
            {
                lst.Add(new JsonString(value));
            }
            else
            {
                lst.Add(new JsonNull());
            }

            return this;
        }

        public ArrayBuilder Add(JsonObject value)
        {
            lst.Add(value);

            return this;
        }

        public ArrayBuilder Add(JsonArray value)
        {
            lst.Add(value);

            return this;
        }

        public ArrayBuilder Add(IJsonValue value)
        {
            lst.Add(value);

            return this;
        }

        public JsonArray Build()
        {
            return new JsonArray(lst);
        }

        public override string ToString()
        {
            return Build().ToString();
        }
    }
}
